package com.simple.accounting.activity;


import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import com.simple.accounting.R;
import com.simple.accounting.adapter.AccountAdapter;
import com.simple.accounting.db.AccountBean;
import com.simple.accounting.db.DBManager;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends BaseActivity {
    private ListView searchLv;
    private SearchView searchView;
    private TextView emptyTv;
    private TextView cancel;
    private TextView income;
    private TextView outcome;
    private TextView countTv;
    List<AccountBean> mAccountBeans;   //数据源
    AccountAdapter adapter;    //适配器对象

    private String keyword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
        mAccountBeans = new ArrayList<>();
        adapter = new AccountAdapter(this, mAccountBeans);
        searchLv.setAdapter(adapter);
        searchLv.setEmptyView(emptyTv);   //设置无数局时，显示的控件

        searchLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                AccountBean clickBean = mAccountBeans.get(position);  //获取正在被点击的这条信息
                //弹出提示用户是否删除的对话框
                showDeleteItemDialog(clickBean);
                return true;
            }
        });
        searchLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(SearchActivity.this, DetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", mAccountBeans.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }


            @SuppressLint("SetTextI18n")
            @Override
            public boolean onQueryTextChange(String s) {
                if (s.length() > 0) {
                    keyword = s;
                    List<AccountBean> list1 = DBManager.getAccountListByRemarkFromAccountTb(s);
                    List<AccountBean> list2 = DBManager.getAccountListByTypeFromAccountTb(s);
                    List<AccountBean> list3 = DBManager.getAccountListByTimeFromAccountTb(s);
                    mAccountBeans.clear();
                    mAccountBeans.addAll(list1);
                    removeExist(list2);
                    removeExist(list3);
                    adapter.notifyDataSetChanged();

                    float incomes = 0;
                    float outcomes = 0;
                    for (int i = 0; i < mAccountBeans.size(); i++) {
                        if (mAccountBeans.get(i).getKind() == 1) {
                            incomes += mAccountBeans.get(i).getMoney();
                        } else {
                            outcomes += mAccountBeans.get(i).getMoney();
                        }
                    }
                    int count = mAccountBeans.size();

                    countTv.setVisibility(View.VISIBLE);
                    countTv.setText("共" + count + "笔");
                    countTv.setTextColor(Color.BLACK);
                    income.setVisibility(View.VISIBLE);
                    income.setText("收入" + incomes);
                    income.setTextColor(Color.BLUE);
                    outcome.setVisibility(View.VISIBLE);
                    outcome.setText("支出" + outcomes);
                    outcome.setTextColor(Color.RED);
                    return true;
                } else {
                    mAccountBeans.clear();
                    adapter.notifyDataSetChanged();
                    countTv.setVisibility(View.INVISIBLE);
                    countTv.setText("共0笔");
                    countTv.setTextColor(Color.BLACK);
                    income.setVisibility(View.INVISIBLE);
                    income.setText("收入");
                    income.setTextColor(Color.BLACK);
                    outcome.setText("支出");
                    outcome.setVisibility(View.INVISIBLE);
                    outcome.setTextColor(Color.BLACK);
                    return false;
                }
            }
        });
    }

    private void removeExist(List<AccountBean> data){
        if(mAccountBeans.size() >0){
            List<AccountBean> tmpList = new ArrayList<>();
            for(int i=0; i< mAccountBeans.size(); i++){
                AccountBean bean = mAccountBeans.get(i);
                if(data.size() >0){
                    for(int j=0; j<data.size(); j++){
                        if(bean.getId() != data.get(j).getId()){
                            tmpList.add(data.get(j));
                        }
                    }
                }
            }
            mAccountBeans.addAll(tmpList);
        }else {
            mAccountBeans.addAll(data);
        }
    }

    /* 弹出是否删除某一条记录的对话框*/
    private void showDeleteItemDialog(final AccountBean clickBean) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息").setMessage("您确定要删除这条记录么？")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int click_id = clickBean.getId();
                        //执行删除的操作
                        DBManager.deleteItemFromAccountTbById(click_id);
                        mAccountBeans.remove(clickBean);   //实时刷新，移除集合当中的对象
                        adapter.notifyDataSetChanged();   //提示适配器更新数据
                        onResume();
                    }
                });
        builder.create().show();   //显示对话框
    }


    @SuppressLint("SetTextI18n")
    @Override
    protected void onResume() {
        super.onResume();
        if (mAccountBeans != null && mAccountBeans.size() > 0) {
            List<AccountBean> list1 = DBManager.getAccountListByRemarkFromAccountTb(keyword);
            List<AccountBean> list2 = DBManager.getAccountListByTypeFromAccountTb(keyword);
            List<AccountBean> list3 = DBManager.getAccountListByTimeFromAccountTb(keyword);
            mAccountBeans.clear();
            mAccountBeans.addAll(list1);

            removeExist(list2);
            removeExist(list3);
            adapter.notifyDataSetChanged();
            float incomes = 0;
            float outcomes = 0;
            for (int i = 0; i < mAccountBeans.size(); i++) {
                if (mAccountBeans.get(i).getKind() == 1) {
                    incomes += mAccountBeans.get(i).getMoney();
                } else {
                    outcomes += mAccountBeans.get(i).getMoney();
                }
            }
            int count = mAccountBeans.size();

            countTv.setText("共" + count + "笔");
            countTv.setTextColor(Color.BLACK);
            income.setText("收入" + incomes);
            income.setTextColor(Color.BLUE);
            outcome.setText("支出" + outcomes);
            outcome.setTextColor(Color.RED);
        }
    }

    private void initView() {
        searchView = findViewById(R.id.searchView);
        searchLv = findViewById(R.id.searchLv);
        emptyTv = findViewById(R.id.empty);
        cancel = findViewById(R.id.tv_cancel);
        income = findViewById(R.id.income);
        outcome = findViewById(R.id.outcome);
        countTv = findViewById(R.id.count);
        searchView.setQueryHint("备注/分类/时间");
        searchView.setIconifiedByDefault(false);
        searchView.setSubmitButtonEnabled(true);//设置搜索提交按钮可见

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

}
